package com.irun.pm.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.ui.PopulationQueryActivity.FugitiveAdapter;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/***违章查询
 * @author wushengxiong
 * @date 2012-5-25
 * @email a610959773@gmail.com
 * @desc 
 */
public class ViolationQueryActivity extends BaseActivity implements OnClickListener, OnItemClickListener{
	
	private List<DriverInfo> listViolation = new ArrayList<DriverInfo>(); //数据存储
	private ViolationAdapter mAdapter;         //适配器
	private EditText etPlateNumber;            //车牌号
	private ListView lvViolation;              //查询列表
	private DriverInfoControllers controller;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.violation_query);
		controller = new DriverInfoControllers(this);
		initAllViews();
	}

	private void initAllViews(){
		listViolation = controller.getDriverInfoList(Utils.TYPE_VIOLATION_QUERY);
		mAdapter = new ViolationAdapter(listViolation);
		lvViolation = (ListView)findViewById(R.id.vq_lv_listView);
		lvViolation.setAdapter(mAdapter);
		lvViolation.setOnItemClickListener(this);
		
		etPlateNumber = (EditText)findViewById(R.id.vq_et_plateNumbers);
		Button btnQuery = (Button)findViewById(R.id.vq_btn_query);
		btnQuery.setOnClickListener(this);
		listViolation = controller.getDriverInfoList(Utils.TYPE_VIOLATION_QUERY);
		mAdapter = new ViolationAdapter(listViolation);
		lvViolation.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.vq_btn_query:
			doProgress("提示", "正在搜索中...", new Callable<String>() {
				@Override
				public String call() throws Exception {
					// TODO Auto-generated method stub
					Thread.sleep(2000);
					return null;
				}
			}, new Callback<String>() {
				@Override
				public void onCallback(String pCallbackValue) {
					// TODO Auto-generated method stub
					String number = etPlateNumber.getText().toString();
					if(StringUtil.isEmpty(number)){
						listViolation = controller.getDriverInfoList(Utils.TYPE_VIOLATION_QUERY);
						mAdapter = new ViolationAdapter(listViolation);
						lvViolation.setAdapter(mAdapter);
						Toast.makeText(ViolationQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
						return;
					}
					listViolation.clear();
					if(StringUtil.isEmpty(number)){
						Toast.makeText(getBaseContext(), getString(R.string.please_input_inquires_the_conditions), Toast.LENGTH_SHORT).show();
					}else if(!StringUtil.isEmpty(number)){  //单个条件车牌号
						listViolation = controller.queryDriverInfoByCardNumber(number, Utils.TYPE_VIOLATION_QUERY);
						if(!listViolation.isEmpty()){
							Toast.makeText(ViolationQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(ViolationQueryActivity.this, "查询不到任何数据", Toast.LENGTH_SHORT).show();
						}
					}
					mAdapter = new ViolationAdapter(listViolation);
					lvViolation.setAdapter(mAdapter);
				}
			}, null);
			break;

		default:
			break;
		}
	}
	
	class ViolationAdapter extends BaseAdapter{
		private List<DriverInfo> listFugitives ;
		
		public ViolationAdapter(List<DriverInfo> list){
			listFugitives = list;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listFugitives.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listFugitives.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				convertView = LayoutInflater.from(ViolationQueryActivity.this)
						.inflate(R.layout.car_item, null);
				holder = new ViewHolder();
				holder.tvName = (TextView)convertView.findViewById(R.id.tvCardNum);
				holder.tvCardNumber = (TextView)convertView.findViewById(R.id.tvTime);
				holder.tvDate = (TextView)convertView.findViewById(R.id.tvName);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(listFugitives.get(position).getName());
			holder.tvCardNumber.setText(listFugitives.get(position).getCardNumber());
			holder.tvDate.setText(listFugitives.get(position).getDate());
			return convertView;
		}
		
	}
	
	class ViewHolder{
		ImageView ivPhoto;
		TextView tvName;
		TextView tvCardNumber;
		TextView tvDate;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		String name = listViolation.get(arg2).getName();
		String cardNumber = listViolation.get(arg2).getCardNumber();
		String date = listViolation.get(arg2).getDate();
		String address = listViolation.get(arg2).getAddress();
		String result = listViolation.get(arg2).getResult();
		String dealPeople = listViolation.get(arg2).getDeal_people();
		String note = listViolation.get(arg2).getNote();
		
		bundle.putString("name", name);
		bundle.putString("cardNumber", cardNumber);
		bundle.putString("date", date);
		bundle.putString("address", address);
		bundle.putString("result", result);
		bundle.putString("dealPeople", dealPeople);
		bundle.putString("note", note);
		
		intent.putExtras(bundle);
		intent.setClass(ViolationQueryActivity.this, ViolationDetailsActivity.class);
		startActivity(intent);
	}
}
